package com.jdjw.modules.thirdpost.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.honeststraightto.entity.HonestStraightTo;
import com.jdjw.modules.honeststraightto.mapper.HonestStraightToMapper;
import com.jdjw.modules.migranttasksign.entity.MigrantTaskSign;
import com.jdjw.modules.migranttasksign.mapper.MigrantTaskSignMapper;
import com.jdjw.modules.thirdpost.param.HonestStraightToParam;
import com.jdjw.modules.thirdpost.param.ThirdPostInParm;
import com.jdjw.utils.CommonUtil;
import com.jdjw.utils.ConstantUtil;
import com.jdjw.utils.DateUtil;
import com.jdjw.utils.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class HonestStraightToImpl extends ServiceImpl<MigrantTaskSignMapper, MigrantTaskSign>
        implements IService<MigrantTaskSign> {

    @Autowired
    private static HonestStraightToMapper honestStraightToMapper;

    @Autowired
    static ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl;

    @Resource
    public void setThirdPostExceptionServiceImpl(ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl) {
        HonestStraightToImpl.thirdPostExceptionServiceImpl = thirdPostExceptionServiceImpl;
    }

    @Resource
    public void setHonestStraightToMapper(HonestStraightToMapper honestStraightToMapper) {
        HonestStraightToImpl.honestStraightToMapper = honestStraightToMapper;
    }


    public static void doPosthonestStraightTo(ThirdPostInParm thirdPostInParm) {
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 27;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 27;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 27;
        }
        try {
            //查询
            QueryWrapper entityWrapper = new QueryWrapper();
            if (StringUtils.isNotBlank(thirdPostInParm.getStartTime())) {
                entityWrapper.ge("bgsj", thirdPostInParm.getStartTime());
            } else if (StringUtils.isNotBlank(thirdPostInParm.getEndTime())) {
                entityWrapper.le("bgsj", thirdPostInParm.getEndTime());
            }
            List<HonestStraightTo> all = honestStraightToMapper.selectList(entityWrapper);
            if (all == null || all.size() < 1) {
                log.info("直办基础表数据库查询数据为0条");
                return;
            }
            if (all.size() > 3000) {
                Collection<Collection<HonestStraightTo>> fgList = CommonUtil.subCollection(all, 2900);
                for (Collection<HonestStraightTo> obj : fgList) {
                    sendData((List<HonestStraightTo>) obj, url);
                }
            } else {
                sendData(all, url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static void sendData(List<HonestStraightTo> all, String url) {
        ResponseResult result = new ResponseResult();
        ArrayList list = new ArrayList();
        for (HonestStraightTo a : all) {
            //组装数据
            HonestStraightToParam param = new HonestStraightToParam();
            BeanUtils.copyProperties(a, param);
//            param.setId(a.getId());
            param.setBiz_id(a.getId());
//            param.setCwh(a.getCwh());
            param.setCwhxzqh(CommonUtil.addZeroForNum(a.getCwhxzqh(), 16));
//            param.setBgr(a.getBgr());
//            param.setBgsj(a.getBgsj());
//            param.setBfyr(a.getBfyr());
//            param.setBfyrsfzh(a.getBfyrsfzh());
//            param.setWtqkms(a.getWtqkms());
//            param.setClr(a.getClr());
//            param.setClyj(a.getClyj());
//            param.setClsj(a.getClsj());
//            param.setZhzjs(a.getZhzjs());
//            param.setSfclwc(a.getSfclwc());
//            param.setSfzwtxs(a.getSfzwtxs());
//            param.setSfdjzwcf(a.getSfdjzwcf());
//            param.setDjzwcflx(a.getDjzwcflx());
//            param.setCllx(a.getCllx());
//            param.setClsj(a.getClsj());
//            param.setBz(a.getBz());
            param.setDel_flag(Objects.equals("1", a.getDelFlag()) ? "0" : "1");
//            param.setCwhxzqh(a.getCwhxzqh());

            String jsonStr = JSONObject.toJSONString(param);
            //转化为json对象
            list.add(JSONObject.parseObject(jsonStr));
        }
        //发送(post)
        net.sf.json.JSONObject jsonObject = HttpRequestUtil.getHttpResponseJsonAllSSL(url, list);
        System.out.println("结果："+jsonObject);
        if (jsonObject != null) {
            if ("200".equals(jsonObject.get("code"))) {
                result.setCode(200);
                result.setMsg("SUCCESS");
                result.setData("推送完成");
                log.info("直报基础表推送成功"+list.size(), result);
            } else {

                //记录日志
                thirdPostExceptionServiceImpl.insertExceptionData(
                        "doPosthonestStraightTo", "直报基础表",
                        jsonObject.getString("message"),
                        DateUtil.formatDate(DateUtil.FORMAT_YMD, new Date().getTime()));
                result.setCode(500);
                result.setMsg("ERROR");
                result.setData(jsonObject.getString("message"));
                log.info("直报基础表推送失败", result);
            }
        } else {
            log.info("直报基础表推送失败", "第三方返回空");
        }
    }


}

